package com.china08.yxyapi.api.manage.tec.teaching.continuedu.mytask;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTask;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskLessonRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 
 * @annotation: 继续教育-我的任务-列表
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyMyTaskSchController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduUserTaskRepository yxyContinueduUserTaskRepository;

    @Autowired
    private YxyContinueduTaskLessonRepository yxyContinueduTaskLessonRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/mytask/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        Pageable pageable = reqModel.getPageable(Direction.DESC, "endDate");
        Page<YxyContinueduUserTask> page = null;
        String userId = getProfile(request, response).getId();
        String taskStatus = reqModel.taskStatus;
        String keyword = reqModel.keyword;

        if (StringUtils.isBlank(keyword)) {
            // 查询我的全部任务
            if (taskStatus.equals("0")) {
                page = yxyContinueduUserTaskRepository.findByUserId(userId, pageable);
            } else// 未完成
            if (taskStatus.equals("1")) {
                page = yxyContinueduUserTaskRepository.findByUserIdAndStatus(userId, -1, pageable);
            } else// 进行中
            if (taskStatus.equals("2")) {
                page = yxyContinueduUserTaskRepository.findByUserIdAndStatus(userId, 0, pageable);
            } else // 已完成
            if (taskStatus.equals("3")) {
                page = yxyContinueduUserTaskRepository.findByUserIdAndStatus(userId, 1, pageable);
            }
        } else {
            // 查询全部任务
            if (taskStatus.equals("0")) {
                page = yxyContinueduUserTaskRepository.findByUserIdAndNameContaining(userId, keyword, pageable);
            } else// 未完成
            if (taskStatus.equals("1")) {
                page = yxyContinueduUserTaskRepository.findByUserIdAndStatusAndNameContaining(userId, -1, keyword,
                        pageable);
            } else// 进行中
            if (taskStatus.equals("2")) {
                page = yxyContinueduUserTaskRepository.findByUserIdAndStatusAndNameContaining(userId, 0, keyword,
                        pageable);
            } else // 已完成
            if (taskStatus.equals("3")) {
                page = yxyContinueduUserTaskRepository.findByUserIdAndStatusAndNameContaining(userId, 1, keyword,
                        pageable);
            }

        }
        Integer i = 1;
        List<RespModel> list = new ArrayList<YxyMyTaskSchController.RespModel>();
        if (page != null && page.getContent().size() > 0) {
            for (YxyContinueduUserTask yxyContinueduUserTask : page) {
                RespModel model = new RespModel();
                model.courseCount = yxyContinueduTaskLessonRepository.countByTaskId(yxyContinueduUserTask.getTaskId());
                model.endDate = DateToolUtils.format(yxyContinueduUserTask.getEndDate(),
                        DateToolUtils.FORMAT_SHORT_YMD);
                model.name = yxyContinueduUserTask.getName();
                model.score = yxyContinueduUserTask.getScore();
                model.taskId = yxyContinueduUserTask.getTaskId();
                model.taskProgress = yxyContinueduUserTask.getProgress();
                model.tStatus = yxyContinueduUserTask.getStatus() == 0 ? "进行中"
                        : (yxyContinueduUserTask.getStatus() == -1 ? "未完成" : "已完成");
                model.num = i;
                list.add(model);
                i++;
            }
        }
        Page<RespModel> pageRes = new PageImpl<RespModel>(list, pageable, page.getTotalElements());

        return pageRes;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 任务状态 0全部 1未完成 2进行中 3已完成
        public String taskStatus;
        // 搜索
        public String keyword;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 任务ID
        public String taskId;
        // 任务名称
        public String name;
        // 学分数
        public Integer score;
        // 课程数
        public Integer courseCount;
        // 截止时间
        public String endDate;
        // 任务状态
        public String tStatus;
        // 任务进度
        public Double taskProgress;
        // 序号
        public Integer num;

    }
}
